﻿<!--#Include File = "CustomTemplate.asp" -->

<%
	Call  PageController.RenderPage()
	Public Function Page_Configure()
		PageController.PageTitle = "User management"
		PageController.AuthorizedRoles =  Array("admin")
		pageController.RequiresAuthentication  = True
		PageController.PageCSSFiles = "main.css" 	
		
	End function
%>
<%Public Function Page_RenderForm()%>
	<H2>User management</H2>
	
	<%	Page.Execute %>
	<Span Class="Caption">List of User<HR></Span>	

	<%gdUser%>	
	
	<hr>
	<a href="UserEdit.asp">Add new</a>
<%End Function%>


<%  'This would normaly go in a another page, but for the sake of simplicity and to minimize the number of pages
    'I'm including code behind stuff here...	
	Dim gdUser
		
	Public Function Page_Init()
		
		Set gdUser = New_ServerDataGrid("gdUser")		
		Page.AutoResetScrollPosition = True
	End Function

	Public Function Page_Controls_Init()						
	End Function
	
	Public Function Page_PreRender()						
		Call ShowRecords()
	End Function
	
Public Sub SetTemplate(obj)		 
	obj.AlternatingItemStyle = "background-color:#dddddd;font-size:8pt;border:1px solid black" 
	obj.ItemStyle = "font-size:8pt;border:1px solid black"
	obj.Control.Style = "width:100%;border-collapse:collapse;"
	obj.HeaderStyle = "font-weight:bold;color:white;background-color:#4682b4;font-size:10pt;font-family:tahoma;border:1px solid black"
	obj.BorderWidth = 1
	obj.BorderColor = "Black"
	obj.GenerateColumns() 'Just so it generates most of them :-)
	obj.CreateColumns Array("id" , "username","fullname", "email", "phone", "status", "isdeleted","") , Array("ID", "Username","Fullname","Email","Phone","Status", "IsDeteled", "Action")
	obj.AutoGenerateColumns = False  'Now lets not have it generate the columns again during render!
	obj.Columns(1).ColumnType = 2    'Modify the column settings we need.
	obj.Columns(1).DataValueField = "id"
	obj.Columns(1).DataTextField = "username"
	obj.Columns(1).DataFormatString = "<A HREF='UserEdit.asp?id={1}' Class='ActionLinkNormal'>{0}</A>"

	obj.Columns(5).ColumnType = 3    'Modify the column settings we need.
	obj.Columns(5).CellRenderFunctionName = "RenderStatusColumn"
	
	
	obj.Columns(7).ColumnType = 3    'Modify the column settings we need.
	obj.Columns(7).HeaderText = "Actions"
	obj.Columns(7).HorizonalAlign = "center"
	obj.Columns(7).CellRenderFunctionName = "RenderActionColumn"
	
	
	obj.Pager.ProgressStyle = "font-size:8pt"
	obj.Pager.PrevNextStyle = "font-size:8pt"
	obj.Pager.PagerStyle    = "font-size:8pt"
	obj.AllowPaging = True
	
	
End Sub
Public Function RenderStatusColumn(ds)
		if ds("status").Value then
			Response.Write("Activate")
		else 
			Response.Write("Un-Active")
		end if
End Function
Public Function RenderActionColumn(ds)
		Response.Write "<A HREF='UserEdit.asp?id=" & ds("id").Value & "' Class='ActionLink'>edit</A> | "
		If ds("isdeleted") <> true Then
			Response.Write "<A " & Page.GetEventScript("HREF","Page","DeleteUser",ds("id").Value,"") & " Class='ActionLink'>delete</a> | "	
		End If
		If ds("status").Value Then
			Response.Write "<A " & Page.GetEventScript("HREF","Page","UnActiveUser",ds("id").Value,"") & " Class='ActionLink'>Un-Active</a>"
			
		Else
			Response.Write "<A " & Page.GetEventScript("HREF","Page","ActiveUser",ds("id").Value,"") & " Class='ActionLink'>Active</a>"
			
		End If
End Function

Public Function Page_DeleteUser(e)
	Dim sSQL
		Dim rs
		sSQL = "SELECT * FROM [user] Where id=" & Replace(e.Instance,"'","''")
		
		Set rs = DBLayer.GetRecordSet(sSQL)		
		If rs.RecordCount = 0 Then
			Exit Function
		End If			
		
		rs("isdeleted").Value	= true	
	
		DBLayer.UpdateRecordSet rs
End Function

Public Function Page_ActiveUser(e)
	Dim sSQL
		Dim rs
		sSQL = "SELECT * FROM [user] Where id=" & Replace(e.Instance,"'","''")
		
		Set rs = DBLayer.GetRecordSet(sSQL)		
		
		If rs.RecordCount = 0 Then
			Exit Function
		End If	
		rs("status").Value	= true
		DBLayer.UpdateRecordSet rs
End Function

Public Function Page_UnActiveUser(e)
	Dim sSQL
		Dim rs
		sSQL = "SELECT * FROM [user] Where id=" & Replace(e.Instance,"'","''")
		
		Set rs = DBLayer.GetRecordSet(sSQL)		
		
		If rs.RecordCount = 0 Then
			Exit Function
		End If	
		rs("status").Value	= false
		DBLayer.UpdateRecordSet rs
End Function

Public Function ShowRecords()
	Dim sSQL
	sSQL = "SELECT id, username, firstname + lastname as fullname,phone, email, status, isdeleted from [user] "
	
	Set gdUser.DataSource = GetRecordset(sSQL)

	SetTemplate gdUser

End Function


		
	
%>